﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DALCar : IDALCar, IDisposable
    {
        private CarBatteryEntities context;
        private bool disposed = false;

        public DALCar(CarBatteryEntities context)
        {
            this.context = context;
        }

        public IEnumerable<Car> GetAllCarsOwnedByCustomer(int customerID)
        {
            var cars = context.Car.Where(e => e.customerID.Equals(customerID));

            return cars.AsEnumerable();
        }

        public Car GetCarByID(int carID)
        {
            return context.Car.FirstOrDefault(x => x.carID.Equals(carID));
        }

        public Car FindCarByLicencePlate(string licencePlate)
        {
            return context.Car.FirstOrDefault(x => x.licencePlate.Equals(licencePlate));
        }

        public void InsertCar(Car car)
        {
            context.Car.Add(car);
        }

        public void DeleteCar(int carID)
        {
            context.Car.Remove(GetCarByID(carID));
        }

        public void UpdateCar(Car car)
        {
            context.Entry(car).State = EntityState.Modified;
        }

        public int Save()
        {
            return context.SaveChanges();

        }

        public virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                    context.Dispose();

                this.disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}